package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;

import controller.BerichtsheftController;
import data.Data;
import data.Lang;
import data.Woche;

public class ExportDialog {
	
	private JDialog dialog;
	
	private JComboBox<Woche> dropdownboxStartWoche;
	private JComboBox<Woche> dropdownboxEndWoche;
	
	private JLabel labelStartWoche;
	private JLabel labelEndWoche;
		
	private BerichtsheftController bhc;
	private Data data;
	
	public ExportDialog(BerichtsheftController bhc, Data data) {
		this.bhc = bhc;
		this.data = data;
		
		dialog = new JDialog();
		dialog.setTitle(Lang.EXPORTDG_TITLE);
		dialog.setSize(520, 180);
		dialog.setResizable(false);
		dialog.setMinimumSize(dialog.getSize());
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setLocationRelativeTo(null);
		dialog.setLayout(null);
		dialog.setModal(true);
		setupGUI();
		dialog.setVisible(true);
		
	}
	
	private void setupGUI() {
		
		dropdownboxStartWoche = new JComboBox<Woche>();
		dropdownboxStartWoche.setBounds(10, 10, 240, 25);
		dropdownboxStartWoche.setMaximumRowCount(15);
		
		for(int i = 0; i < data.getWochenCount(); i++) {
			dropdownboxStartWoche.addItem(data.getWoche(i));
		}
		
		dialog.add(dropdownboxStartWoche);
		
		labelStartWoche = new JLabel();
		labelStartWoche.setBounds(10, 40, 240, 70);
		labelStartWoche.setVerticalAlignment(JLabel.TOP);
		labelStartWoche.setText("<html><body>" + ((Woche)dropdownboxStartWoche.getSelectedItem()).getFullDescriptorString().replaceAll("\n", "<br>") + "</body></html>");
		dialog.add(labelStartWoche);
		
		dropdownboxStartWoche.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED){
					Woche selectedWeek = (Woche) e.getItem();
					labelStartWoche.setText("<html><body>" + selectedWeek.getFullDescriptorString().replaceAll("\n", "<br>") + "</body></html>");
					dropdownboxEndWoche.removeAllItems();
					for(int i = dropdownboxStartWoche.getSelectedIndex(); i < data.getWochenCount(); i++) {
						dropdownboxEndWoche.addItem(data.getWoche(i));
					}
				}
			}
		});
		
		dropdownboxEndWoche = new JComboBox<Woche>();
		dropdownboxEndWoche.setBounds(265, 10, 240, 25);
		dropdownboxEndWoche.setMaximumRowCount(15);
		
		for(int i = 0; i < data.getWochenCount(); i++) {
			dropdownboxEndWoche.addItem(data.getWoche(i));
		}
		
		dialog.add(dropdownboxEndWoche);
		
		labelEndWoche = new JLabel();
		labelEndWoche.setBounds(265, 40, 240, 70);
		labelEndWoche.setVerticalAlignment(JLabel.TOP);
		labelEndWoche.setText("<html><body>" + ((Woche)dropdownboxEndWoche.getSelectedItem()).getFullDescriptorString().replaceAll("\n", "<br>") + "</body></html>");
		dialog.add(labelEndWoche);
		
		dropdownboxEndWoche.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED){
					Woche selectedWeek = (Woche) e.getItem();
					labelEndWoche.setText("<html><body>" + selectedWeek.getFullDescriptorString().replaceAll("\n", "<br>") + "</body></html>");
				    
				}
			}
		});
		
		JButton buttonExport = new JButton(Lang.EXPORTDG_BTN_EXPORT);
		buttonExport.setBounds(415, 115, 90, 25);
		buttonExport.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int indexStart = 0;
				Woche startWoche = (Woche) dropdownboxStartWoche.getSelectedItem();
				while(data.getWoche(indexStart).getWochenNummer() != startWoche.getWochenNummer()) {
					indexStart++;
				}
				int indexEnd = indexStart;
				Woche endWoche =  (Woche) dropdownboxEndWoche.getSelectedItem();
				while(data.getWoche(indexEnd).getWochenNummer() != endWoche.getWochenNummer()) {
					indexEnd++;
				}
				
				bhc.exportDocX(indexStart, indexEnd);
				dialog.dispose();
			}
		});
		dialog.add(buttonExport);
		
	}
	
}
